package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.ProdProp;
import cn.s1995.sloth.bizs.entity.ProdPropValue;
import cn.s1995.sloth.bizs.service.ProdPropService;
import cn.s1995.sloth.bizs.service.ProdPropValueService;
import cn.s1995.sloth.common.core.util.StringUtil;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 产品属性
 *
 * @author Lex
 * @date 2023-12-15 00:51:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/prodProp")
@Tag(description = "ProdProp", name = "BMS-产品属性管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProdPropController {

    private final ProdPropService prodPropService;
    private final ProdPropValueService prodPropValueService;

    /**
     * 通过id查询产品属性
     *
     * @param id id
     * @return R
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_ProdProp_view')")
    public R getById(@PathVariable Long id) {
        return R.ok(prodPropService.getById(id));
    }

    /**
     * 查询详情
     *
     * @param query 查询信息
     * @return 字典信息
     */
    @ApiOperationSupport(order = 2)
    @GetMapping("/details")
    public R getDetails(@ParameterObject ProdProp query) {
        return R.ok(prodPropService.getOne(Wrappers.query(query), false));
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param req  产品属性
     * @return
     */
    @ApiOperationSupport(order = 3)
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_ProdProp_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject ProdProp req) {
        LambdaQueryWrapper<ProdProp> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(req.getRule()), ProdProp::getRule, req.getRule());
        wrapper.like(StrUtil.isNotBlank(req.getPropName()), ProdProp::getPropName, req.getPropName());
        return R.ok(prodPropService.page(page, wrapper));
    }

    /**
     * 通过产品属性id查找产品属性值
     *
     * @param prodPropId 产品属性id
     * @return 同类型
     */
    @ApiOperationSupport(order = 4)
    @GetMapping("/{prodPropId}/values")
    public R<List<ProdPropValue>> getProdPropValuesById(@PathVariable Long prodPropId) {
        return R.ok(prodPropValueService.list(
                Wrappers.lambdaQuery(ProdPropValue.class)
                        .eq(ProdPropValue::getPropId, prodPropId)
                        .orderByAsc(ProdPropValue::getValueName)));
    }

    /**
     * 新增产品属性
     *
     * @param req 产品属性
     * @return R
     */
    @ApiOperationSupport(order = 10)
    @Operation(summary = "新增产品属性", description = "新增产品属性")
    @SysLog("新增产品属性")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdProp_add')")
    public R save(@Valid @RequestBody ProdProp req) {
        prodPropService.save(req);
        return R.ok(req);
        // return prodPropService.saveSpec(req);
    }

    /**
     * 通过id删除产品属性
     *
     * @param ids propId列表
     * @return R
     */
    @ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除产品属性", description = "通过id删除产品属性")
    @SysLog("通过id删除产品属性")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdProp_del')")
    public R remove(@RequestBody Long[] ids) {
        return prodPropService.removeSpes(CollUtil.toList(ids));
    }

    /**
     * 修改产品属性
     *
     * @param req 产品属性
     * @return R
     */
    @ApiOperationSupport(order = 20)
    @Operation(summary = "修改产品属性", description = "修改产品属性")
    @SysLog("修改产品属性")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_ProdProp_edit')")
    public R updateById(@RequestBody ProdProp req) {
        prodPropService.updateById(req);
        return R.ok(req);
    }

    /**
     * 获取所有的规格
     */
    @ApiOperationSupport(order = 5)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    public R<List<ProdProp>> getList(String name) {
        return R.ok(prodPropService.list(
                Wrappers.lambdaQuery(ProdProp.class)
                        .like(StringUtil.isNotEmpty(name), ProdProp::getPropName, name)
                // .eq(ProdProp::getRule, BizCst.ProdProp.Rule.sale)
        ));
    }

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param req
     * @return
     */
    @GetMapping("/item/page")
    public R getProdPropValuePage(Page page, ProdPropValue req) {
        return R.ok(prodPropValueService.page(page, Wrappers.query(req)));
    }

    /**
     * 通过id查询产品属性值
     * @param id id
     * @return R
     */
    @GetMapping("/item/details/{id}")
    public R getProdPropValueById(@PathVariable Long id) {
        return R.ok(prodPropValueService.getById(id));
    }

    /**
     * 查询产品属性值详情
     * @param query 查询条件
     * @return R
     */
    @GetMapping("/item/details")
    public R getProdPropValueDetails(ProdPropValue query) {
        return R.ok(prodPropValueService.getOne(Wrappers.query(query), false));
    }

    /**
     * 新增产品属性值
     * @param req 产品属性值
     * @return R
     */
    @SysLog("新增产品属性值")
    @PostMapping("/item")
    public R save(@RequestBody ProdPropValue req) {
        return R.ok(prodPropValueService.save(req));
    }

    /**
     * 修改产品属性值
     * @param req 产品属性值
     * @return R
     */
    @SysLog("修改产品属性值")
    @PutMapping("/item")
    public R updateById(@RequestBody ProdPropValue req) {
        return R.ok(prodPropValueService.updateById(req));
    }

    /**
     * 通过id删除产品属性值
     * @param id id
     * @return R
     */
    @SysLog("删除产品属性值")
    @DeleteMapping("/item/{id}")
    public R removeProdPropValueById(@PathVariable Long id) {
        return R.ok(prodPropValueService.removeById(id));
    }


    @ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_ProdProp_export')")
    public List<ProdPropValue> export(ProdPropValue req) {
        return prodPropValueService.list(Wrappers.query(req));
    }
}
